using JGSY.CMS.LowCode.Platform.Application.DTOs;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// Elasticsearch服务接口
    /// 提供全文搜索和索引管理功能
    /// </summary>
    public interface IElasticsearchService
    {
        /// <summary>
        /// 初始化索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>是否成功</returns>
        Task<bool> InitializeIndexAsync(string indexName);

        /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>是否成功</returns>
        Task<bool> DeleteIndexAsync(string indexName);

        /// <summary>
        /// 检查索引是否存在
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>是否存在</returns>
        Task<bool> IndexExistsAsync(string indexName);

        /// <summary>
        /// 索引文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="document">文档对象</param>
        /// <param name="id">文档ID</param>
        /// <returns>是否成功</returns>
        Task<bool> IndexDocumentAsync<T>(string indexName, T document, string? id = null) where T : class;

        /// <summary>
        /// 批量索引文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="documents">文档列表</param>
        /// <returns>成功索引的文档数量</returns>
        Task<int> BulkIndexDocumentsAsync<T>(string indexName, IEnumerable<T> documents) where T : class;

        /// <summary>
        /// 更新文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="id">文档ID</param>
        /// <param name="document">文档对象</param>
        /// <returns>是否成功</returns>
        Task<bool> UpdateDocumentAsync<T>(string indexName, string id, T document) where T : class;

        /// <summary>
        /// 删除文档
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="id">文档ID</param>
        /// <returns>是否成功</returns>
        Task<bool> DeleteDocumentAsync(string indexName, string id);

        /// <summary>
        /// 获取文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="id">文档ID</param>
        /// <returns>文档对象</returns>
        Task<T?> GetDocumentAsync<T>(string indexName, string id) where T : class;

        /// <summary>
        /// 搜索文档
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="indexName">索引名称</param>
        /// <param name="query">搜索查询</param>
        /// <param name="from">起始位置</param>
        /// <param name="size">返回数量</param>
        /// <returns>搜索结果</returns>
        Task<(IEnumerable<T> Documents, long TotalCount)> SearchAsync<T>(
            string indexName, 
            string query, 
            int from = 0, 
            int size = 20) where T : class;

        /// <summary>
        /// 高级搜索
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="searchRequest">搜索请求</param>
        /// <returns>搜索结果</returns>
        Task<(IEnumerable<SearchItemDto> Items, long TotalCount)> AdvancedSearchAsync(
            string indexName, 
            ElasticsearchSearchRequest searchRequest);

        /// <summary>
        /// 聚合查询
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <param name="aggregationName">聚合名称</param>
        /// <param name="field">聚合字段</param>
        /// <param name="size">返回数量</param>
        /// <returns>聚合结果</returns>
        Task<Dictionary<string, long>> AggregateAsync(
            string indexName, 
            string aggregationName, 
            string field, 
            int size = 10);

        /// <summary>
        /// 获取索引统计信息
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>统计信息</returns>
        Task<ElasticsearchIndexStats> GetIndexStatsAsync(string indexName);

        /// <summary>
        /// 健康检查
        /// </summary>
        /// <returns>是否健康</returns>
        Task<bool> HealthCheckAsync();

        /// <summary>
        /// 刷新索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>是否成功</returns>
        Task<bool> RefreshIndexAsync(string indexName);

        /// <summary>
        /// 清空索引
        /// </summary>
        /// <param name="indexName">索引名称</param>
        /// <returns>是否成功</returns>
        Task<bool> ClearIndexAsync(string indexName);

        /// <summary>
        /// 重建索引
        /// </summary>
        /// <param name="sourceIndex">源索引名称</param>
        /// <param name="targetIndex">目标索引名称</param>
        /// <returns>是否成功</returns>
        Task<bool> ReindexAsync(string sourceIndex, string targetIndex);
    }

    /// <summary>
    /// Elasticsearch搜索请求
    /// </summary>
    public class ElasticsearchSearchRequest
    {
        /// <summary>
        /// 搜索关键词
        /// </summary>
        public string? Query { get; set; }

        /// <summary>
        /// 搜索字段
        /// </summary>
        public List<string>? Fields { get; set; }

        /// <summary>
        /// 过滤条件
        /// </summary>
        public Dictionary<string, object>? Filters { get; set; }

        /// <summary>
        /// 排序字段
        /// </summary>
        public List<ElasticsearchSort>? Sort { get; set; }

        /// <summary>
        /// 起始位置
        /// </summary>
        public int From { get; set; } = 0;

        /// <summary>
        /// 返回数量
        /// </summary>
        public int Size { get; set; } = 20;

        /// <summary>
        /// 是否启用高亮
        /// </summary>
        public bool EnableHighlight { get; set; } = true;

        /// <summary>
        /// 高亮字段
        /// </summary>
        public List<string>? HighlightFields { get; set; }

        /// <summary>
        /// 聚合配置
        /// </summary>
        public Dictionary<string, object>? Aggregations { get; set; }

        /// <summary>
        /// 最小匹配度
        /// </summary>
        public string? MinimumShouldMatch { get; set; }

        /// <summary>
        /// 模糊查询参数
        /// </summary>
        public int? Fuzziness { get; set; }
    }

    /// <summary>
    /// Elasticsearch排序配置
    /// </summary>
    public class ElasticsearchSort
    {
        /// <summary>
        /// 排序字段
        /// </summary>
        public string Field { get; set; } = string.Empty;

        /// <summary>
        /// 排序方向 (asc/desc)
        /// </summary>
        public string Order { get; set; } = "desc";

        /// <summary>
        /// 排序模式
        /// </summary>
        public string? Mode { get; set; }

        /// <summary>
        /// 缺失值处理
        /// </summary>
        public string? Missing { get; set; }
    }

    /// <summary>
    /// Elasticsearch索引统计信息
    /// </summary>
    public class ElasticsearchIndexStats
    {
        /// <summary>
        /// 索引名称
        /// </summary>
        public string IndexName { get; set; } = string.Empty;

        /// <summary>
        /// 文档总数
        /// </summary>
        public long DocumentCount { get; set; }

        /// <summary>
        /// 索引大小（字节）
        /// </summary>
        public long SizeInBytes { get; set; }

        /// <summary>
        /// 分片数
        /// </summary>
        public int ShardCount { get; set; }

        /// <summary>
        /// 副本数
        /// </summary>
        public int ReplicaCount { get; set; }

        /// <summary>
        /// 索引状态
        /// </summary>
        public string Status { get; set; } = string.Empty;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreatedAt { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime LastUpdated { get; set; }
    }
}
